import { defineStore } from "pinia";
const user_key = 'login'
const user_time = 'user_time'
const useValidTime = 6*60*60*1000;
export const loginStore = defineStore('login', {
    state: () => {
        return {
            userInfo: null,
            isinClassRoom:false,
            ansArrs:null
        }
    },
    getters: {
        getUserInfo(){
        let users="";
        if(typeof(this.userInfo) == "object"){
            users = this.userInfo;
        }else{
            users = JSON.parse(this.userInfo);
        }
        return users
          //   if(!this.userInfo){
          //   }
        //     let info = JSON.parse(localStorage.getItem(user_key));
        //     if(info){
        //         let strtime =  localStorage.getItem(user_time)
        //         if(strtime){
        //             let vaildtime = Number(strtime) + useValidTime
        //             let timespan = Date.parse(new Date())
        //             if(timespan <=  vaildtime){
        //                 console.log("用户未过期")
        //                 return info
        //             }else{
        //                 localStorage.setItem(user_key, null);
        //                 this.userInfo = null
        //             }
        //         }
        //     }
        //   return info
        },
        
    },
    actions: {
        addUser(data) {
            this.userInfo = data;
           // localStorage.setItem("login", data);
            let timespan = Date.parse(new Date())
            localStorage.setItem(user_time, timespan);
        },
        changInclassRoom(data){
            this.isinClassRoom = data;
        },
        AddansArrs(data){
            const a = [];
            data.forEach((k,v) => {
                a.push({
                    0:v,
                    1:k
                })
            });
            this.ansArrs=a;
        }
        
    },
    persist:true
})